package scanner;

/**
 * 
 * @author Hornhaut
 * 
 */
public class Scanner {

	private final int PLUS = 0;
	private final int MINUS = 1;
	private final int MULT = 2;
	private final int DIV = 3;
	private final int POT = 4;
	private final int BRA_OP = 5;
	private final int BRA_CL = 6;
	private final int NUMBER = 7;

	private final int NOERROR = 10;
	private final int INPUTERROR = 11;
	private final int COMMAERROR = 12;

	private  int i;

	/**
	 * 
	 * @param input
	 */
	public void scanString(String input) {

		ScannerOut so = new ScannerOut();

		for (i = 0; i < (input.length() - 1); i++) {

			so = readChar(input);
			System.out.println("token: " + so.getToken() + " lexem: "
					+ so.getLexem() + " Error: " + so.getErrorCode());
		}

	}

	/**
	 * 
	 * @param input
	 * @return
	 */
	private ScannerOut readChar(String input) {

		ScannerOut so = new ScannerOut();
		int j;

		while (input.charAt(i) == ' ') {
			i++;
		}

		switch (input.charAt(i)) {
		/*
		 * case ' ': { while (input.charAt(i+1) == ' '){ i++; } break; }
		 */
		case '+': {
			so.setToken(PLUS);
			so.setLexem("+");
			so.setErrorCode(NOERROR);
			break;
		}
		case '-': {
			so.setToken(MINUS);
			so.setLexem("-");
			so.setErrorCode(NOERROR);
			break;
		}
		case '*': {
			so.setToken(MULT);
			so.setLexem("*");
			so.setErrorCode(NOERROR);
			break;
		}
		case '/': {
			so.setToken(DIV);
			so.setLexem("/");
			so.setErrorCode(NOERROR);
			break;
		}
		case '^': {
			so.setToken(POT);
			so.setLexem("^");
			so.setErrorCode(NOERROR);
			break;
		}
		case '(': {
			so.setToken(BRA_OP);
			so.setLexem("(");
			so.setErrorCode(NOERROR);
			break;
		}
		case ')': {
			so.setToken(BRA_CL);
			so.setLexem(")");
			so.setErrorCode(NOERROR);
			break;
		}
		default: {

			if (String.valueOf(input.charAt(i)).matches("\\d")) {

				boolean comma = false;

				so.setToken(NUMBER);
				so.setLexem(String.valueOf(input.charAt(i)));

				j = i+1;

				while (j < input.length()) {
					if((String.valueOf(input.charAt(j)).matches("\\d")) || (input
							.charAt(j) == '.'))
					{
						i = j;
						if ((input.charAt(j) == '.') && (comma == false)) {
							comma = true;
						} else if ((input.charAt(j) == '.') && (comma == true)) {
							so.setErrorCode(COMMAERROR);
							//so.setLexem("");
							break;
						}
						so.setLexem(so.getLexem() + String.valueOf(input.charAt(j)));
						j++;
						so.setErrorCode(NOERROR);
						
					}
					else{
						break;
					}
				}
			}

			else {
				so.setErrorCode(INPUTERROR);
				so.setLexem(String.valueOf(input.charAt(i)));
			}
			break;
		}
		}
		return so;
	}

}
